package com.todod.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.todod.dto.AuthorityDto;
import com.todod.dto.RoleDto;
import com.todod.entity.QueryEntry;
import com.todod.entity.ResData;
import com.todod.entity.ResultData;
import com.todod.model.Department;
import com.todod.model.ProjectInfo;
import com.todod.model.Role;
import com.todod.model.RoleAuthority;
import com.todod.model.RoleMenu;
import com.todod.model.RoleProject;
import com.todod.model.User;
import com.todod.service.IAuthorityService;
import com.todod.service.IDepartmentService;
import com.todod.service.IRoleAuthorityService;
import com.todod.service.IRoleMenuService;
import com.todod.service.IRoleProjectService;
import com.todod.service.IRoleService;
import com.todod.service.IUserService;
import com.todod.service.ProjectInfoService;

@RestController
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private IRoleService iRoleService;
	@Autowired
	private IRoleMenuService iRoleMenuService;
	@Autowired
	private IRoleAuthorityService iRoleAuthorityService;
	@Autowired
	private IAuthorityService iAuthorityService;
	@Autowired
	private IDepartmentService service;
	@Autowired
	private IUserService iUserService;
	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	public ProjectInfoService projectInfoService;
	@Autowired
	public IRoleProjectService iRoleProjectService;

	@RequestMapping("/getList")
	public ResultData getList(@RequestBody QueryEntry queryEntry, @RequestAttribute Long _userId) {
		List<Long> list2 = service.getdepatrtid(iUserService.getById(_userId));
		queryEntry.setDepertid(list2);
		IPage<RoleDto> list = iRoleService.getList(queryEntry);
		User byId = iUserService.getById(_userId);
		Long id = service.getById(byId.getDepartid()).getId();
		List<Department> departments = service.getdepart3(id);
		HashMap<Object, Object> hashMap = new HashMap<>();
		hashMap.put("list", list);
		hashMap.put("departments", departments);
		return ResultData.success("ok", hashMap);
	}

	/**
	 * 通过部门id获取本部门以及部门以下的的角色
	 * 
	 * @param departid
	 * @return
	 */
	@GetMapping("/getAllList1")
	public ResultData getAllList1(Long departid) {
		return ResultData.success();
	}

	/**
	 * 通过部门id获取部门下的角色
	 * 
	 * @param departid
	 * @return
	 */
	@GetMapping("/getAllList")
	public ResultData getAllList(Long departid) {
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sr_department_id", departid);
		List<Role> list = iRoleService.list(queryWrapper);

		return ResultData.success("ok", list);
	}

	/**
	 * 通过部门id获取部门下的角色
	 *
	 * @param departid
	 * @return
	 */
	@GetMapping("/getNewAllList")
	public ResultData getNewAllList(Long departid) {
		Department department = iDepartmentService.getDepartmentById(departid);
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sr_department_id", department.getId());
		List<Role> list = iRoleService.list(queryWrapper);
		return ResultData.success("ok", list);
	}

	@RequestMapping("/save")
	public ResultData save(@RequestAttribute Long _userId, @RequestBody Role role) {
		iRoleService.save(_userId, role);
		return ResultData.success("ok");
	}

	/**
	 * 保存角色菜单关联
	 * 
	 * @Title: saveMenu
	 * @Description:
	 * @author gsh
	 * @date 2020年11月23日
	 * @param _userId
	 * @param resData
	 * @return
	 */
	@PostMapping("/saveMenu")
	public ResultData saveMenu(@RequestAttribute Long _userId, @RequestBody ResData resData) {
		iRoleMenuService.saveData(_userId, resData.getRoleId(), resData.getMenuIds());
		return ResultData.success("ok");
	}

	/**
	 * 获取角色菜单关联
	 * 
	 * @Title: getRoleMenuList
	 * @Description:
	 * @author gsh
	 * @date 2020年11月23日
	 * @param _userId
	 * @param resData
	 * @return
	 */
	@GetMapping("/getRoleMenuList")
	public ResultData getRoleMenuList(Long roleId) {
		List<RoleMenu> list = iRoleMenuService.getByRoleId(roleId);
		for (RoleMenu roleMenu : list) {
			System.out.println(roleMenu);
		}
		return ResultData.success("ok", list);
	}

	/**
	 * 获取角色拥有的权限
	 * 
	 * @Title: getHaveAuthorit
	 * @Description:
	 * @author gsh
	 * @date 2020年11月24日
	 * @param roleId
	 * @return
	 */
	@GetMapping("/getRoleAuthorit")
	public ResultData getRoleAuthorit(Long roleId) {
		List<AuthorityDto> list = iAuthorityService.getAuthorityListByRoleId(roleId);
		List<RoleAuthority> list1 = iRoleAuthorityService.getRoleAuthority(roleId);
		List<Long> hashAuthId = list1.stream().map(x -> x.getAuthorityId()).collect(Collectors.toList());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("hashAuthId", hashAuthId);
		return ResultData.success("ok", map);
	}

	/**
	 * 保存权限
	 * 
	 * @Title: saveAuthorit
	 * @Description:
	 * @author gsh
	 * @date 2020年11月24日
	 * @param _userId
	 * @param resData
	 * @return
	 */
	@PostMapping("/saveAuthorit")
	public ResultData saveAuthorit(@RequestAttribute Long _userId, @RequestBody ResData resData) {
		iRoleAuthorityService.saveData(_userId, resData.getRoleId(), resData.getRoleAuthoritys());
		return ResultData.success("ok");
	}

	/**
	 * 获取角色拥有的项目
	 * 
	 * @Title: getRoleProject
	 * @Description:
	 * @author dyl
	 * @date 2023-07-05
	 * @param roleId
	 * @return
	 */
	@PostMapping("/getRoleProject")
	public ResultData getRoleProject(Long roleId) {
		List<ProjectInfo> list = projectInfoService.getAllList();
		List<RoleProject> list1 = iRoleProjectService.getRoleProject(roleId);
		List<Long> hashAuthId = list1.stream().map(x -> x.getProjectId()).collect(Collectors.toList());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("hashProjectId", hashAuthId);
		return ResultData.success("ok", map);
	}

	/**
	 * 保存权限
	 * 
	 * @Title: saveProject
	 * @Description:
	 * @author dyl
	 * @date 2023-07-05
	 * @param _userId
	 * @param resData
	 * @return
	 */
	@PostMapping("/saveProject")
	public ResultData saveProject(@RequestAttribute Long _userId, @RequestBody ResData resData) {
		iRoleProjectService.saveData(_userId, resData.getRoleId(), resData.getProjectIds());
		return ResultData.success("ok");
	}

}
